package at.ac.tuwien.isis.infret.ue2.tests;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import at.ac.tuwien.isis.infret.helper.DocumentContainer;
import at.ac.tuwien.isis.infret.ue2.ArffParser;
import at.ac.tuwien.isis.infret.ue2.QueryHandler;

public class QueryHandlerTest {
	QueryHandler test;
	@Before
	public void setUp() throws Exception {
		
		test = new QueryHandler(
				"res"+File.separatorChar+
				"tests"+File.separatorChar+
				"test.arff");
		/*		
		test = new QueryHandler(
				"res"+File.separatorChar+
				"features_20news"+File.separatorChar+
				"news_5grams_001_04_stm_stw.arff");
				*/
	}

	@After
	public void tearDown() throws Exception {
	}
	@Test
	public void testApplyQuery() {
		Vector<String> query = new Vector<String>();
		query.add("Iris-setosa/doc1");
		query.add("Iris-setosa/doc2");
		test.SimilarityFunctionFlag = "l2";
		TreeMap<DocumentContainer,Double> result = test.applyQuery(query, 2);
		DocumentContainer dummyRef1 = new DocumentContainer("Iris-setosa","doc3",null);
		DocumentContainer dummyRef2 = new DocumentContainer("Iris-setosa","doc4",null);
		assertTrue(result.containsKey(dummyRef2) && result.containsKey(dummyRef1));
		test = new QueryHandler(
				"res"+File.separatorChar+
				"tests"+File.separatorChar+
				"test.arff");
		//System.out.println(result);
		test.SimilarityFunctionFlag = "cosine";
		result = test.applyQuery(query, 2);
		assertTrue(result.containsKey(dummyRef2) && result.containsKey(dummyRef1));
	}
	@Test
	public void testgetQueryContainer() {
		Vector<String> query = new Vector<String>();
		query.add("Iris-setosa/doc1");
		Vector<DocumentContainer> queryContainer = test.getQueryContainer(query);
		Iterator<DocumentContainer> iter = queryContainer.iterator();
		while(iter.hasNext()){
			DocumentContainer doc = iter.next();
			assertEquals("doc1", doc.getDocumentname());
			assertEquals("Iris-setosa", doc.getClassname());
		}
	}
	@Test
	public void testApplyL2SimilarityFunction(){
		Vector<String> query = new Vector<String>();
		query.add("Iris-setosa/doc1");
		query.add("Iris-setosa/doc2");
		Vector<DocumentContainer> queryContainer = test.getQueryContainer(query);
		//System.out.println(Arrays.toString(queryContainer.get(0).getAttributes().toDoubleArray()));
		//System.out.println(Arrays.toString(queryContainer.get(1).getAttributes().toDoubleArray()));

		assertEquals(new Double(0.5),test.applyL2SimilarityFunction(
				queryContainer.get(0), queryContainer.get(1)));
	}
	@Test
	public void testApplyCosieSimilarityFunction(){
		Vector<String> query = new Vector<String>();
		query.add("Iris-setosa/doc2");
		query.add("Iris-setosa/doc3");
		Vector<DocumentContainer> queryContainer = test.getQueryContainer(query);
		//System.out.println(queryContainer.get(0).getAttributes().toDoubleArray()[3]);
		assertEquals(new Double(0.0),test.applyCosineSimilarityFunction(
				queryContainer.get(0), queryContainer.get(1)));
	}
}
